/*
 * @Author: yao
 * @Date: 2024-10-31 10:23:32
 * @LastEditTime: 2024-11-16 00:39:05
 * @LastEditors: Michael丶Yao 825862865@qq.com
 * @Description:
 * @FilePath: \jdev-web\src\router\index.ts
 */
import { createRouter, createWebHistory } from "vue-router";
import constantRoutes from "@/router/constant-routes";
import { useConfirm } from "primevue/useconfirm";

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});


export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !["Login", "Home", "NotFound", "404"].includes(name)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

router.onError((error, to) => {
  if (error?.message.includes("Failed to fetch dynamically imported module")) {
    const Confirm = useConfirm();
    Confirm.require({
      header: "温馨提示",
      message: "系统已升级,请刷新",
      icon: "pi pi-info-circle",
      rejectProps: {
        style: "display:none",
      },
      acceptProps: {
        size: "small",
      },
      acceptLabel: "刷新",
      accept: () => {
        window.location.reload();
      },
    });
  } else {
    console.error("router-onError: ", error, to);
  }
});
export default router;
